/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lansupportserver;

import ObjectHandler.LanSupportMessage;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;

/**
 *
 * @author Quan
 */
public final class ViewScreen extends javax.swing.JFrame {

    private Socket sendSocket;
    private ImagePanel imagePanel;

    /**
     * Creates new form ViewScreen
     */
    public ViewScreen(Socket sendSocket) {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        imagePanel = new ImagePanel(null);
        this.getContentPane().add(imagePanel);
        this.sendSocket = sendSocket;
    }

    public void SendRequest() {
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.StartViewScreen);
        lanSupportMessage.Send(sendSocket);
    }

    public void changeImage(byte[] imageData) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
            Iterator<?> readers = ImageIO.getImageReadersByFormatName("png");
            ImageReader reader = (ImageReader) readers.next();
            Object source = bis;
            ImageInputStream iis = ImageIO.createImageInputStream(source);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Image image = reader.read(0, param);
            imagePanel.setImg(image);
        } catch (IOException ex) {
            CloseConnection();
        }
    }

    public void CloseConnection() {
        LanSupportMessage lanSupportMessage = new LanSupportMessage(LanSupportMessage.StopViewScreen);
        lanSupportMessage.Send(sendSocket);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        CloseConnection();
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
